/**
 * @Copyright：<a href="www.wisesoft.net.cn">四川川大智胜系统集成有限公司</a>
 * @Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
 * @CreateTime: 2022年08月03日  20:02
 */
package com.fxsen.commons.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.net.URL;

/**
 *@Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
 *@CreateTime: 2022年08月03日  20:02
 */
public class ResourceUtils {
    //传输协议file:
    public static final String PROTOCOL_FILE = "file:";
    //传输协议classpath*:
    public static final String PROTOCOL_CLASSPATH = "classpath*:";
    /**
     * 通过path,Class得到InputStream
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/3 20:03
 	 * @param: path
 	 * @param: class1
 	 * @return: {@link InputStream}
    **/
    public static InputStream getResourceAsStream(String path, Class class1){
        InputStream inputstream = class1.getResourceAsStream(path);
        return inputstream;
    }
    /**
     * 通过path得到InputStream
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/3 20:03
 	 * @param: path
 	 * @return: {@link InputStream}
    **/
    public static InputStream getResourceAsStream(String path)
    {
        String trip = path.startsWith("/") ? path.substring(1) : path;
        InputStream stream = null;

        if(!StringUtils.isEmpty(trip))
        {
            stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(trip);

            if(stream == null)
            {
                stream = ResourceUtils.class.getClassLoader().getResourceAsStream(trip);
            }

            if(stream == null)
            {
                stream = ResourceUtils.class.getResourceAsStream(trip);
            }
        }

        return stream;
    }
    /**
     * 通过path得到URL
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/3 20:04
 	 * @param: path
 	 * @return: {@link URL}
    **/
    public static URL getResource(String path)
    {
        String trip = path.startsWith("/") ? path.substring(1) : path;
        URL url = null;

        if(!StringUtils.isEmpty(trip))
        {
            url = Thread.currentThread().getContextClassLoader().getResource(trip);

            if(url == null)
            {
                url = ResourceUtils.class.getClassLoader().getResource(trip);
            }

            if(url == null)
            {
                url = ResourceUtils.class.getResource(trip);
            }
        }

        return url;
    }
}
